package cn.bytecollege.service;

import cn.bytecollege.util.ValidCode;
import lombok.extern.java.Log;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.Random;

@Service
@Log4j2
public class GetEmailService {
    @Resource
    JavaMailSender mailSender;
    public String getEmail(String email){
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper helper = null;
        String validCode = ValidCode.getValidCode();
        try {
            helper = new MimeMessageHelper(mimeMessage, true);
            //标题
                helper.setSubject("疫时达邮箱验证码");
                //
                helper.setText("您好！您的验证码为："+"<h2>"+validCode+"</h2>"+"千万不能告诉别人哦！",true);
                //
                helper.setTo(email);
                //
                helper.setFrom("muzi5201012@qq.com");
                mailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
            return null;
        }
        return validCode;
    }

}
